<?php

namespace ApiBundle\Controller\V1;

use ApiBundle\Controller\BaseController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

/**
 * @author  coffey  <coffey@nligo.com>
 *
 * 游戏记录类
 * Class GameNperRecordController
 * @package ApiBundle\Controller\V1
 */
class GameNperRecordController extends BaseController
{
    /**
     * @author  coffey
     * 获取彩票记录列表
     *
     * @ApiDoc(
     *  resource=true,
     *  description="获取彩票记录列表",
     *  parameters={
     *      {"name"="userToken", "dataType"="string","required"=true,"description"="用户token"},
     *      {"name"="limit", "dataType"="integer","required"=true,"description"="条数"},
     *  },
     * )
     * @Rest\POST("/gamerecord/list")
     */
    public function listAction(Request $request)
    {
        $param['userToken'] = $request->request->get('userToken','');
        $param['limit'] = $request->request->getInt('limit',0);
        $tokenData = $this->checkToken($param['userToken']);
        if(isset($tokenData['code']) && $tokenData['code'] == 1)
        {
            $userInfo   =   $tokenData['userinfo'] ? $tokenData['userinfo'] : array();
            if(!isset($userInfo['userId']) || empty($userInfo['userId']))
            {
                return $this->FinalJson(5,array());
            }
        }
        else
        {
            return $this->FinalJson($tokenData['errcode']);
        }
        $result['list'] = $this->getGamesNperRecordRepository()->getNperList($param['limit']);
        $result['count'] = count($result['list']);
        return $this->FinalJson(0,$result);
    }

    /**
     * @author  coffey
     * 获取当前最新期号信息
     *
     * @ApiDoc(
     *  resource=true,
     *  description="获取当前最新期号接口",
     *  parameters={
     *      {"name"="userToken", "dataType"="string","required"=true,"description"="用户token"},
     *      {"name"="limit", "dataType"="integer","required"=true,"description"="limit参数"},
     *  },
     * )
     * @Rest\POST("/gamerecord/nownper")
     */
    public function nownperAction(Request $request)
    {
        $param['userToken'] = $request->request->get('userToken','');
        $param['limit'] = $request->request->getInt('limit',0);
        $tokenData = $this->checkToken($param['userToken']);
        if(isset($tokenData['code']) && $tokenData['code'] == 1)
        {
            $userInfo   =   $tokenData['userinfo'] ? $tokenData['userinfo'] : array();
            if(!isset($userInfo['userId']) || empty($userInfo['userId']))
            {
                return $this->FinalJson(5,array());
            }
        }
        else
        {
            return $this->FinalJson($tokenData['errcode']);
        }
        $result['list'] = $this->getGamesNperRecordRepository()->getNperList($param['limit']);
        $result['count'] = count($result['list']);
        return $this->FinalJson(0,$result);
    }

    /**
     * @author  coffey
     * 游戏历史记录
     *
     * @ApiDoc(
     *  resource=true,
     *  description="游戏历史记录",
     *  parameters={
     *      {"name"="userToken", "dataType"="string","required"=true,"description"="用户token"},
     *      {"name"="limit", "dataType"="integer","required"=true,"description"="limit参数"},
     *      {"name"="type", "dataType"="integer","required"=false,"description"="类型 0为本期 1为本周 2上周"},
     *  },
     * )
     * @Rest\POST("/gamerecord/openbet")
     */
    public function openbetAction(Request $request)
    {
        $param['userToken'] = $request->request->get('userToken','');
        $param['limit'] = $request->request->getInt('limit',0);
        $param['type'] = $request->request->getInt('type',0);
        $tokenData = $this->checkToken($param['userToken']);
        if(isset($tokenData['code']) && $tokenData['code'] == 1)
        {
            $userInfo   =   $tokenData['userinfo'] ? $tokenData['userinfo'] : array();
            if(!isset($userInfo['userId']) || empty($userInfo['userId']))
            {
                return $this->FinalJson(5,array());
            }
        }
        else
        {
            return $this->FinalJson($tokenData['errcode']);
        }

        $list = $this->getGamesUserOrdrRepository()->loadOrder($param['type'],$param['limit']);
        return $this->FinalJson(0,$list);
    }

}
